<template>
  <div class="stock-warning-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="产品">
        <el-select v-model="queryParams.product" placeholder="请选择产品">
          <el-option v-for="item in products" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="stockWarnings" border style="width: 100%">
      <el-table-column prop="product.name" label="产品名称" width="180"></el-table-column>
      <el-table-column prop="product.code" label="产品编码" width="180"></el-table-column>
      <el-table-column prop="current_stock" label="当前库存" width="120"></el-table-column>
      <el-table-column prop="warning_stock" label="预警库存" width="120"></el-table-column>
      <el-table-column prop="difference" label="差值" width="100"></el-table-column>
      <el-table-column prop="create_time" label="创建时间"></el-table-column>
    </el-table>

    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :current-page="queryParams.page"
      :page-size="queryParams.size"
      @current-change="handlePageChange"
    ></el-pagination>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import request from '@/utils/request'
import { ElMessage } from 'element-plus'

const stockWarnings = ref([])
const total = ref(0)
const queryParams = ref({
  page: 1,
  size: 10,
  product: ''
})
const products = ref([])

// 获取产品列表
const getProducts = async () => {
  const res = await request.get('/api/product/')
  products.value = res.records
}

// 获取库存预警列表
const getList = async () => {
  const res = await request.get('/api/stock-warning/', {
    params: queryParams.value
  })
  stockWarnings.value = res.records
  total.value = res.total
}

// 分页变化
const handlePageChange = (newPage) => {
  queryParams.value.page = newPage
  getList()
}

onMounted(() => {
  getProducts()
  getList()
})
</script>

<style scoped>
.stock-warning-container {
  padding: 20px;
}
</style>
